package com.example.sm.mycook.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sm.mycook.R;
import com.example.sm.mycook.adapter.HomeRVAdapter;
import com.example.sm.mycook.beans.HomeMyRVBean;
import com.example.sm.mycook.beans.HomeRVBean;
import com.example.sm.mycook.interfaceCallBack.HomeJsonCallBack;
import com.example.sm.mycook.interfaceCallBack.HomeRVDataIF;
import com.example.sm.mycook.task.HomeDataTask;
import com.example.sm.mycook.widget.HomeRefreshRecycleView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2 {

    private String path="http://api5.wochu.cn/client/v1/actsamend?parameters=%7B%22source%22%3A%22A%22%2C%22version%22%3A%224.2.0%22%7D";

    private View view;
    private TextView home_location;
    private ImageView home_search;
    private HomeRefreshRecycleView home_recyclerView;
    private RecyclerView recyclerView;
    private HomeRVAdapter adapter;
//    private List<HomeRVBean.DataBean.ActsBean.ItemsBean> list=new ArrayList<>();

    private List<HomeMyRVBean> myList=new ArrayList<>();

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_home, container, false);

        initView();
        initClick();

        new HomeDataTask(new HomeJsonCallBack() {
            @Override
            public void sendDataForHome(List<HomeMyRVBean> list) {

                for (int i = 0; i < list.size(); i++) {
                    HomeMyRVBean homeMyRVBean=list.get(i);
                    myList.add(homeMyRVBean);
                }
                myList.add(0,null);
//                myList.add(1,null);

                adapter=new HomeRVAdapter(getActivity(),myList);
                recyclerView.setAdapter(adapter);
                RVItemClick();




            }
        },getActivity()).execute(path);





        MyGridLayoutManager();

        return view;
    }

    private void initLoadData() {


        //
        //再惠双12：
        //http://api5.wochu.cn/client/v1/actsamend?parameters=%7B%22source%22%3A%22A%22%2C%22version%22%3A%224.2.0%22%7D


//        Retrofit retrofit=new Retrofit.Builder()
//                .baseUrl("http://api5.wochu.cn/client/")
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//
//        HomeRVDataIF homeRVDataIF=retrofit.create(HomeRVDataIF.class);
//
//        homeRVDataIF.getData().enqueue(new Callback<HomeRVBean>() {
//            @Override
//            public void onResponse(Call<HomeRVBean> call, Response<HomeRVBean> response) {
//
//                HomeRVBean.DataBean dataBean=response.body().getData();
//                List<HomeRVBean.DataBean.ActsBean> actsBeenList=dataBean.getActs();
//                for (int i = 0; i < actsBeenList.size(); i++) {
//
//                    HomeRVBean.DataBean.ActsBean actsBean=actsBeenList.get(i);
//                   List<HomeRVBean.DataBean.ActsBean.ItemsBean> itemsBeanList=actsBean.getItems();
//                    for (int i1 = 0; i1 < itemsBeanList.size(); i1++) {
//                        HomeRVBean.DataBean.ActsBean.ItemsBean itemsBean=itemsBeanList.get(i1);
//                        list.add(itemsBean);
//
//                    }
//
//                }
//                getActivity().runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Log.e("AAA","-------->"+"设置适配器");
//                        adapter=new HomeRVAdapter(getActivity(),list);
//
//                        recyclerView.setAdapter(adapter);
//
//                        RVItemClick();
//                    }
//                });
//
//
//
//            }
//
//            @Override
//            public void onFailure(Call<HomeRVBean> call, Throwable t) {
//
//                Log.e("AAAA","数据下载失败");
//            }
//        });

    }

    private void RVItemClick()
    {
        adapter.setOnItemClickCallBack(new HomeRVAdapter.OnItemClickCallBack() {
            @Override
            public void OnItemClickListener(View view, int pos) {
                if (pos!=0) {
                    String tType = myList.get(pos).gettType();
                    int Ttype=Integer.parseInt(tType);
                    if (Ttype==7||Ttype==5)
                    {
                        return;
                    }

//                    Toast.makeText(getActivity(),"你点击了"+pos,Toast.LENGTH_SHORT ).show();
                }


            }
        });
    }

    private void initClick() {
        home_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        home_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void initView() {
        home_location = ((TextView) view.findViewById(R.id.home_location));
        home_search = ((ImageView) view.findViewById(R.id.home_search));
        home_recyclerView = ((HomeRefreshRecycleView) view.findViewById(R.id.home_recyclerView));
        recyclerView=home_recyclerView.getRefreshableView();
        home_recyclerView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        home_recyclerView.setOnRefreshListener(this);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        handler.sendEmptyMessageAtTime(1,3000);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }

    private void MyGridLayoutManager() {
        GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),12);

        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int size=6;
                switch (adapter.getItemViewType(position))
                {
                    case 0:
                        size=4;

                        break;
                    case  1:

                        size=12;

                        break;
                    case 2:

                        size=12;

                        break;
//                    case 3:
//
//                        size=12;
//                        break;

                    case  4:
                        size=12;
                        break;
                }
                return  size;
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);

    }

    private Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 1:
                    home_recyclerView.onRefreshComplete();
                    break;
            }
        }
    };

}
